package cn.kgc.ssm.web.controller;

import cn.kgc.ssm.web.annotations.Logs;
import cn.kgc.ssm.web.entity.User;
import cn.kgc.ssm.web.enums.Operations;
import cn.kgc.ssm.web.form.LoginForm;
import cn.kgc.ssm.web.response.ResponseResult;
import cn.kgc.ssm.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2024/7/26
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

   // 用户认证
    @GetMapping("login")
    public ResponseResult  login(LoginForm loginForm, HttpSession session){
      return   userService.login(loginForm,session);
    }


    // 用户数据分页
    @GetMapping("page")
    public ResponseResult  userPage(Integer page,Integer limit,String username,Integer age){

        return   userService.userPage(page,limit,username,age);
    }


    // 添加用户信息
    @PostMapping("add")
    public ResponseResult  addUser(User user){

        return   userService.addUser(user);
    }


    // 编辑用户信息
    @PostMapping("edit")
    @Logs(operation = Operations.UPDATE,model = "用户模块")
    public ResponseResult  editUser(User user){
        return   userService.editUser(user);
    }


    // 删除用户信息  ids=1&ids=2   ids[]=1 & ids[] = 2
    @PostMapping("delete")
    public ResponseResult  deleteUser(Integer[]  ids){
        return  userService.batchDelete(ids);
    }

    // 密码重置
    @PostMapping("resetPassword")
    public ResponseResult  resetPassword(Integer id){
        return  userService.resetPassword(id);
    }

    // 根据用户名获取用户的个人信息
    @GetMapping("personInfo/{username}")
    public ResponseResult getPersonInfo(@PathVariable("username") String username){
        return  userService.getPersonInfo(username);
    }

    // 编辑用户的个人信息
    @PostMapping("editPersonalInfo")
    public ResponseResult editPersonInfo(User user){
        return  userService.editUser(user);
    }

    // 个人密码修改
    @PostMapping("editPersonalPassword")
    public ResponseResult editPersonalPassword(String oldPassword,String newPassword ,HttpSession session ){
        return  userService.editPersonalPassword(oldPassword,newPassword,session);
    }


    // 退出登录
    @GetMapping("logout")
    public ResponseResult logout(HttpSession session ){
        // 销毁session对象
        session.invalidate();
        return  ResponseResult.success();
    }
}
